<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Learning About Backbone.js View</title>

	<script type="text/javascript"
		src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	<script type="text/javascript"
		src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
	<script type="text/javascript"
		src="http://documentcloud.github.com/backbone/backbone-min.js"></script>



	<script type="text/javascript">

	Backbone.emulateHTTP = true; // Use _method parameter rather than using DELETE and PUT methods
	Backbone.emulateJSON = true; // Send data to server via parameter rather than via request content


	function getModifiedDate() {
		var d = new Date();
	    var curr_date = d.getDate();
	    var curr_month = d.getMonth() + 1; //Months are zero based
	    var curr_year = d.getFullYear();
	    
         return curr_date+"/"+curr_month+"/"+curr_year + " "+d.getHours()+":"+ d.getMinutes() + ":"+d.getSeconds();
	}
	 
	var Person = Backbone.Model.extend({
	    initialize: function() {
	        this.on('all', function(e) { console.log(this.get('name') + " event: " + e); });
	    },
	    defaults: {
	        name: 'undefined',
	        age: 'undefined'
	    },
	    urlRoot: "./backbone.php",
	    url: function() {
	        var base = this.urlRoot || (this.collection && this.collection.url) || "/";
	        if (this.isNew()) return base;
		   
	        return base + "?id=" + encodeURIComponent(this.id);
	    }
	});
	 
	
	 
	/*
	var person = new Person({id:137});
	person.fetch(); // fetch model from DB with id = 1
	console.log(person);
	
	
	person = new Person({name:"Joe Zim Inicial", age:29});
	person.save(); // create and save a new model on the server, also get id back and set it

	var d = new Date();
	person = new Person({id:106, name: "fecha de modificacion : " + getModifiedDate(), age:5});
	person.save(); // update the model on the server (it has an id set, therefore it is on the server already)

	person = new Person({id:105});
	person.destroy(); // delete the model from the server
	*/

	
	var People = Backbone.Collection.extend({
	    initialize: function() {
	        this.on('all', function(e) { console.log("People event: " + e); });	      
	    },
	    model: Person,
	    url: "./backbone.php"
	});    
	 
	var people = new People();
	people.fetch({    // Get all models for this collection
	    success: function(){
    	    console.log("------------------  Iniciando recuperacion de datos --------------");
	        console.log(people.length);
	        console.log(people.toJSON());
	        console.log("------------------  Finalizando recuperacion de datos --------------");
	    }
	});

	///Esta funcion es util si se desea elegir el metodo de persistencia (BD, LocalStorage)
	Backbone.sync = function(method, model, options) {
	    console.log("------------------  Iniciando sincronizacion con BD --------------");
	    console.log(method, model, options);
	    options.success(model);
	    console.log("------------------  Finalizando sincronizacion con BD --------------");
	};
	
	
	people.create({name:"Joe Zim collection", age:23}); // Create model, add to Collection and add to DB
	people.create({id:137,name:"fecha de modificacion : " + getModifiedDate(), age:72}); // Update model: add to Collection, update DB
	people.create({id:138,name:"2nd modificado  : " + getModifiedDate(), age:72}); // Update model: add to Collection, update DB


	
</script>

</head>
<body>




</body>
</html>